﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace BBInterfaceNET.ProjectExplorer.Model
{
    public class ProjectTree
    {
        public ProjectTree()
        {
            Nodes = new ObservableCollection<ProjectNodeBase>();
        }
        //holds the path for the project tree
        //this is the directory in which all project files reside
        public string Path { get; set; }
        //the collection of project files
        public ObservableCollection<ProjectNodeBase> Nodes { get; set; }

        public void GetNodes(int level, List<ProjectNodeBase> nodes)
        {
            GetNodes(level, Nodes[0], nodes, 0);
        }

        private void GetNodes(int level, ProjectNodeBase parent, List<ProjectNodeBase> nodes, int currLvl)
        {
            if (level == currLvl)
            {
                nodes.AddRange(parent.Children);
                return;
            }
            else
            {
                for (int i = 0; i < parent.Children.Count; i++)
                {
                    GetNodes(level, parent.Children[i], nodes, currLvl + 1);
                }
            }
        }
        public void AddNode(ProjectNodeBase parent, ProjectNodeBase childToAdd)
        {
            childToAdd.Parent = parent;
            if (childToAdd is DirectoryNode)
            {
                int idx = -1;
                for (int i = 0; i < parent.Children.Count; i++)
                {
                    if (parent.Children[i] is DirectoryNode)
                    {
                        idx = i;
                    }
                }
                parent.Children.Insert(idx + 1, childToAdd);
            }
            else
            {
                parent.Children.Add(childToAdd);
            }
        }
    }
}
